#include "../../include/CompareController.h"

/**
 * 构造函数
*/
CompareController::CompareController() {}
/**
 * 析构函数
*/
CompareController::~CompareController() {
    clearMap(compareMap);
}
/**
 * 添加匹配
*/
Compare* CompareController::addCompare(Student* stu,SmartCar* car) {
    Compare* compare = new Compare(stu->getId(),car->getId());
    compareMap[compare->id] = compare;
    return compare;
}
/**
 * 添加匹配
*/
Compare* CompareController::addCompare(std::string _id,std::string _stuId,std::string _carId) {
    Compare* newCompare = new Compare(_id,_stuId,_carId);
    compareMap[_id] = newCompare;
    return newCompare;
}
/**
 * 获取匹配数量
*/
int CompareController::getCompareCount() {
    return compareMap.size();
}
/**
 * 列出所有匹配
*/
void CompareController::listAllCompare(Compare* (&compareList)[]) {
    std::unordered_map<std::string,Compare*>::iterator iter;
    int idx = 0;
    for(iter=compareMap.begin();iter!=compareMap.end();iter++) {
        compareList[idx] = iter->second;
        idx += 1;
    }
}
/**
 * 获取匹配数量-根据小车ID
*/
int CompareController::getCompareCountByCarId(std::string carId) {
    int count = 0;
    std::unordered_map<std::string,Compare*>::iterator iter;
    for(iter=compareMap.begin();iter!=compareMap.end();iter++) {
        if(iter->second->carId == carId) {
            count += 1;
        }
    }
    return count;
}
/**
 * 查找匹配信息-根据小车ID
*/
void CompareController::findCompareByCarId(std::string carId,Compare* (&compareList)[]) {
    std::unordered_map<std::string,Compare*>::iterator iter;
    int idx = 0;
    for(iter=compareMap.begin();iter!=compareMap.end();iter++) {
        if(iter->second->carId == carId) {
            compareList[idx] = iter->second;
            idx += 1;
        }
    }
}
/**
 * 删除匹配-通过Id
*/
bool CompareController::deleteCompare(std::string _id) {
    if(compareMap.find(_id) == compareMap.end()) {
        return false;
    }
    delete compareMap[_id];
    compareMap.erase(_id);
    return true;
}
/**
 * 删除匹配-通过匹配类指针
*/
bool CompareController::deleteCompare(Compare* _compare) {
    if(compareMap.find(_compare->id) == compareMap.end()) {
        return false;
    }
    compareMap.erase(_compare->id);
    delete compareMap[_compare->id];
    return true;
}
/**
 * 从字符串中解析出匹配类
*/
Compare* CompareController::strToCompare(std::istringstream &iss) {
    char split = ' ';
	std::string compareId,studentId,smartCarId;		// 接收缓冲区
    // 接收ID
    std::getline(iss, compareId, split);
    std::getline(iss, studentId, split);
    std::getline(iss, smartCarId, split);
    Compare* compare = this->addCompare(compareId,studentId,smartCarId);
    return compare;
}
